<?php

class Tr extends AbstractSingleton {

    /**
     * @return Tr
     */
    protected static function getInstance() { return parent::doGetInstance(get_class()); }

    private $locale;
    private $hSystemMessages = array();
    private $hUserMessages = array();

    protected function __construct() {
        $this->hUserMessages = array();
        $this->setLocate('ru');
    }

    private function setLocate($localeName) {

        $iniFile = dirname(__FILE__) . '/tr/' . $localeName . '.ini';
        File::checkExistsAndReadable($iniFile);

        $this->hSystemMessages = parse_ini_file($iniFile, true);

        $this->locale = $localeName;
    }

//    public static function str($str) { return self::getInstance()->do_str($str); }
//
//    private function do_str($str) {
//
//        if (!isset($this->hSystemMessages[$str]))
//            return $str;
//
//        return $this->hSystemMessages[$str];
//    }

    public static function month($num) { return self::getInstance()->getMessage('month', $num, "Month #$num"); }
    public static function ofMonth($num) { return self::getInstance()->getMessage('of_months', $num, "Month #$num"); }
    public static function uploadError($num) { return self::getInstance()->getMessage('upload_errors', $num); }

    private function getMessage($section, $str, $default = null) {

        if (!isset($this->hSystemMessages[$section][$str])) {

            if ($default === null)
                throw new Exception("No message '$str' in section '$section'");

            return $default;
        }

        return $this->hSystemMessages[$section][$str];
    }
}

?>